// pages/school/index.js
const App = getApp()
const { Toast } = require("../../config/modal.js");
const utils = require("../../utils/util.js")
Page({

    /**
     * 页面的初始数据
     */
    data: {
        title: "新生有礼",
        window: {},
        bodyBgColor: "#fdfcfc",
        capsuleBarHeight: App.$getMenuButtonHeight(),
        isAuth: false,
        copyImg: "/static/img/copy.png",
        file: {
            maxSize: 5242880,
            maxCount: 1,
            list: []
        },
        form: {
            code: "",
            path: "",
            count: 0,
        },
        userInfo: {
            realName: '-',
            schoolName: '-',
            userPhone: '-'
        },
    },
    URL: {
        save: "/newStudent/save",
        path: "/newStudent/getLetterPath",
        delete: "/newStudent/delete",
        count: "/newStudent/count"
    },
    page: {
        userInfoEdit: "/pages/tabbar/user/userInfoEdit/index",
        home: "/pages/tabbar/index/index",
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        // 检查是否被邀请进来的
        App.$handlerInviteCode(options);
    },

    /**
     *tabbar 高度
     */
    getTabbarHeight(e) {
        let tabbarHeight = e.detail.height
        this.setData({
            tabbarHeight: tabbarHeight
        })
    },


    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
    },


    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        let userInfo = App.$getUserInfo();
        let isAuth = App.$isAuth();
        this.setData({
            userInfo: userInfo,
            isAuth: isAuth
        })
        this.getCount();
        this.getLetter();
    },

    // 获取录取通知书
    getLetter() {
        if (this.data.isAuth) {
            App.$get(true, this.URL.path, {}, res => {
                if (res.R && res.data != null) {
                    this.setData({
                        'form.path': utils.isInvalid(res.data.letterPath) ? '' : res.data.letterPath,
                        'file.list': [{ url: res.data.letterPath, name: "-" }],
                        'form.code': utils.isInvalid(res.data.giftCode) ? '' : res.data.giftCode.substring(0, 5) + "...",
                    })
                }
            })
        }
    },
    getCount() {
        if (this.data.isAuth) {
            App.$get(true, this.URL.count, {}, res => {
                if (res.R) {
                    this.setData({
                        'form.count': res.data,
                    })
                }
            })
        }
    },

    delete() {
        if (this.data.isAuth) {
            App.$delete(true, this.URL.delete, {}, res => {
                if (res.R) {
                    this.setData({
                        'form.path': '',
                        'form.code': '',
                        'file.list': []
                    })
                }
            })
        }
    },
    save() {
        if (this.data.isAuth) {
            // 处理图片 转base64
            if (this.data.file.list.length <= 0) {
                Toast("请选择证件照");
                return;
            }
            let imgs = this.data.file.list
            let imgBase64 = utils.fileToBase64Sync(imgs[0].url);
            App.$post(true, this.URL.save, { letterPath: imgBase64 }, res => {
                if (res.R) {
                    this.getLetter();
                }
            })
        } else {
            Toast("请先授权");
        }
    },


    // 文件读取前
    fileBefore(event) {
        event.detail.callback(true)
    }

    , fileOversize(event) {
        Dialog.alert({
            message: '图片每张只允许5M以内的照片',
            width: "250px",
            confirmButtonColor: "#D2691E",
            confirmButtonText: "关闭",
        }).then(() => {
        });
    },

    fileDelete(event) {
        let index = event.detail.index;
        let files = this.data.file.list;
        files.splice(index, 1);
        this.setData({
            "file.list": files,
        })
    }

    // 证件选择
    , afterRead(event) {
        // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
        const files = event.detail.file;
        let sourceFiles = this.data.file.list;
        files.forEach(file => {
            sourceFiles.push({ url: file.url, name: "" })
        })
        this.setData({
            "file.list": sourceFiles
        })
    },

    goBack() {
        let pages = getCurrentPages();
        let prePage = utils.getFromPage(App, pages);
        App.$removeStore((pages[pages.length - 1]).route);
        App.$redirectTo(prePage, false);
    },
    goHome() {
        App.$switchTab(this.page.home, false)
    },

    copyCode() {
        let orderNo = "吃喝玩乐券";
        wx.setClipboardData({
            data: orderNo,
            success: function (res) {
                wx.getClipboardData({
                    success: function () {
                        wx.showToast({
                            title: '已复制',
                            icon: 'none'
                        })
                    }
                })
            }
        })
    },

    toAuth() {
        App.$navigateTo(this.page.userInfoEdit, true);
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        if (this.data.tabbarActive < 2) {
            this.initData(this.data.tabbarActive, true)
            return;
        }
        wx.stopPullDownRefresh();
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    }


    /**
     * 用户点击右上角分享
     */
    , onShareAppMessage(e) {
        return App.$onShare('/pages/newStudent/index', null, "新生有礼")
    }
})
